package tests;

import java.awt.Color;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import net.sf.hibernate.HibernateException;
import sql.CallRawDB;
import dao.Client;
import dao.Monster;
import dao.Room;
import dao.UserCharacter;

public class Tests_Database {

	/**
	 * @param args
	 * @throws HibernateException 
	 */
	public static void main(String[] args) throws Exception {
		
		List<Exception> excepts = new ArrayList<Exception>();
		HashMap<String, Boolean> tests = new HashMap<String, Boolean>();
		
		//ROOM
		//CREATE
		Room r = new Room();
		r.setIdroom(1);
		Room r1 = new Room();
		r1.setIdroom(2);
		Room r2 = new Room();
		r2.setIdroom(3);
		Room r3 = new Room();
		r3.setIdroom(4);
		Room r4 = new Room();
		r4.setIdroom(5);
		Room r5 = new Room();
		r5.setIdroom(6);
		Room r6 = new Room();
		r6.setIdroom(7);
		tests.put("createRoom1", test_createRoom(r, excepts));
		tests.put("createRoom2", test_createRoom(r1, excepts));
		tests.put("createRoom3", test_createRoom(r2, excepts));
		tests.put("createRoom4", test_createRoom(r3, excepts));
		tests.put("createRoom5", test_createRoom(r4, excepts));
		tests.put("createRoom6", test_createRoom(r5, excepts));
		tests.put("createRoom7", test_createRoom(r6, excepts));
		//GETALLROOMS
		tests.put("getallRooms1", test_getAllRooms(excepts));
		//DELETEROOM
		tests.put("deleteRoom1", test_deleteRoom(r, excepts));
		tests.put("deleteRoom2", test_deleteRoom(r1, excepts));
		//GETALLROOMS
		tests.put("getallRooms2", test_getAllRooms(excepts));
		//UPDATE
		//DEPRECIATED
		
		//TODO
		//LINKROOM
		//CREATE
		//GETALLLINKROOMS
		//DELETE
		//UPDATE
		
		
		
		//MONSTER
		//CREATE
		Monster m = new Monster();
		m.setCurrentRoom(r2);
		m.setNameMonster("monstre test");
		
		Monster m1 = new Monster();
		m1.setCurrentRoom(r3);
		m1.setNameMonster("monstre test1");

		Monster m2 = new Monster();
		m2.setCurrentRoom(r4);
		m2.setNameMonster("monstre test2");

		tests.put("createMonster1", test_createMonster(m, excepts));
		tests.put("createMonster2", test_createMonster(m1, excepts));
		tests.put("createMonster3", test_createMonster(m2, excepts));
		//GETALLMONSTER
		tests.put("getallMonsters1", test_getAllMonsters(excepts));
		//UPDATE
		tests.put("updateMonster1", test_updateMonster(m, excepts));
		tests.put("updateMonster2", test_updateMonster(m1, excepts));
		tests.put("updateMonster3", test_updateMonster(m2, excepts));
		//DELETE
		tests.put("deleteMonster1", test_deleteMonster(m, excepts));
		tests.put("deleteMonster2", test_deleteMonster(m1, excepts));
		tests.put("deleteMonster3", test_deleteMonster(m2, excepts));
		//GETALLROOMS
		tests.put("getallMonster2", test_getAllMonsters(excepts));

		
		
		//CLIENT
		//CREATE
		Client cli = new Client();
		cli.setLogin("toto");
		cli.setPassword("totopasswd");
		cli.setEmail("toto@to.to");
		Client cli1 = new Client();
		cli1.setLogin("titi");
		cli1.setPassword("titipasswd");
		cli1.setEmail("titi@to.to");
		Client cli2 = new Client();
		cli2.setLogin("tata");
		cli2.setPassword("tatapasswd");
		cli2.setEmail("tata@to.to");
		Client cli3 = new Client();
		cli3.setLogin("tutu");
		cli3.setPassword("tutupasswd");
		cli3.setEmail("tutu@to.to");
		Client cli4 = new Client();
		cli4.setLogin("tyty");
		cli4.setPassword("tytypasswd");
		cli4.setEmail("tyty@to.to");
		tests.put("createClient1", test_createClient(cli, excepts));
		tests.put("createClient2", test_createClient(cli1, excepts));
		tests.put("createClient3", test_createClient(cli2, excepts));
		tests.put("createClient4", test_createClient(cli3, excepts));
		tests.put("createClient5", test_createClient(cli4, excepts));
		//GETALLCLIENTS
		tests.put("getallClients1", test_getAllClients(excepts));
		//UPDATE
		tests.put("updateClient1", test_updateClient(cli, excepts));
		tests.put("updateClient2", test_updateClient(cli1, excepts));
		tests.put("updateClient3", test_updateClient(cli3, excepts));
		//DELETE
		tests.put("deleteClient1", test_deleteClient(cli3, excepts));
		tests.put("deleteClient2", test_deleteClient(cli4, excepts));
		//GETALLCLIENTS
		tests.put("getallClients2", test_getAllClients(excepts));

		
		//USERCHARACTER
		//CREATE
		UserCharacter uc = new UserCharacter();
		uc.setPseudo("toto");
		uc.setLife(1);
		uc.setDamage(2);
		uc.setArmor(3);
		uc.setPeau(new Color(3, 4, 5));
		uc.setHabits(new Color(7, 148, 148));
		uc.setYeux(new Color(7, 148, 148));
		uc.setCheveux(new Color(7, 148, 148));
		uc.setSexe("M");
		uc.currentRoom = r2;
		uc.setOwner(cli);
		UserCharacter uc1 = new UserCharacter();
		uc1.setPseudo("titi");
		uc1.setLife(10);
		uc1.setDamage(0);
		uc1.setArmor(0);
		uc1.setPeau(new Color(7, 148, 148));
		uc1.setHabits(new Color(7, 148, 148));
		uc1.setYeux(new Color(7, 148, 148));
		uc1.setCheveux(new Color(7, 148, 148));
		uc1.setSexe("M");
		uc1.currentRoom = r3;
		uc1.setOwner(cli1);
		UserCharacter uc2 = new UserCharacter();
		uc2.setPseudo("tata");
		uc2.setLife(8);
		uc2.setDamage(0);
		uc2.setArmor(10);
		uc2.setPeau(new Color(7, 148, 148));
		uc2.setHabits(new Color(7, 148, 148));
		uc2.setYeux(new Color(7, 148, 148));
		uc2.setCheveux(new Color(7, 148, 148));
		uc2.setSexe("F");
		uc2.currentRoom = r2;
		uc2.setOwner(cli2);
		tests.put("createUserCharacter1", test_createUserCharacter(uc, excepts));
		tests.put("createUserCharacter2", test_createUserCharacter(uc1, excepts));
		tests.put("createUserCharacter3", test_createUserCharacter(uc2, excepts));
		//GETALLUSERCHARACTER
		tests.put("getallUserCharacters1", test_getAllUserCharacters(excepts));
		//UPDATE
		tests.put("updateUserCharacter1", test_updateUserCharacter(uc, excepts));
		tests.put("updateUserCharacter2", test_updateUserCharacter(uc1, excepts));
		//DELETE
		tests.put("deleteUserCharacter1", test_deleteUserCharacter(uc2, excepts));
		//GETALLUSERCHARACTER
		tests.put("getallUserCharacters2", test_getAllUserCharacters(excepts));
		
		
		
		
		int nb_tests_OK = 0;
		int nb_tests_KO = 0;
		// shows all the tests if they are OK or KO
		for (String test : tests.keySet()) {
			String result = (tests.get(test)) ? "OK" : "KO";
			if (result.equals("OK")) {
				nb_tests_OK++;
			} else if (result.equals("KO")) {
				nb_tests_KO++;
			}
			System.out.println(test + " " + result);
		}
		System.out.println("Nb tests OK : " + nb_tests_OK + "/"
				+ tests.keySet().size());
		System.out.println("Nb tests KO : " + nb_tests_KO + "/"
				+ tests.keySet().size());

		for (Exception exception : excepts) {
			System.out.println(exception.getClass());
			exception.printStackTrace();
		}
	}
	
	public static boolean test_createRoom(Room r, List<Exception> expts) {
		try {
			CallRawDB cr = new CallRawDB();
			cr.createRoom(r);
			return true;
		} catch (Exception e) {
			expts.add(e);
			return false;
		}
	}
	
	public static boolean test_deleteRoom(Room r, List<Exception> expts) {
		try {
			CallRawDB cr = new CallRawDB();
			cr.deleteRoom(r);
			return true;
		} catch (Exception e) {
			expts.add(e);
			return false;
		}
	}
	
	public static boolean test_getAllRooms(List<Exception> expts) {
//		try {
//			CallRawDB cr = new CallRawDB();
//			List<Room> list = cr.getAllRooms();
//			if(list != null && list.size()!=0) {
//				return true;
//			} else {
//				return false;
//			}
//			
//		} catch (Exception e) {
//			expts.add(e);
//			return false;
//		}
		return true;
	}
	
	public static boolean test_createMonster(Monster m, List<Exception> expts) {
		try {
			CallRawDB cr = new CallRawDB();
			cr.createMonster(m);
			return true;
		} catch (Exception e) {
			expts.add(e);
			return false;
		}
	}
	
	public static boolean test_updateMonster(Monster m, List<Exception> expts) {
		m.setArmor(-12);
		m.setIdMonster(-1);
		try {
			CallRawDB cr = new CallRawDB();
			cr.updateMonster(m);
			return true;
		} catch (Exception e) {
			expts.add(e);
			return false;
		}
	}
	
	public static boolean test_deleteMonster(Monster m, List<Exception> expts) {
		try {
			CallRawDB cr = new CallRawDB();
			cr.deleteMonster(m);
			return true;
		} catch (Exception e) {
			expts.add(e);
			return false;
		}
	}
	
	public static boolean test_getAllMonsters(List<Exception> expts) {
		try {
			CallRawDB cr = new CallRawDB();
			List<Monster> list = cr.getAllMonsters();
			if(list != null && list.size()!=0) {
				return true;
			} else {
				return false;
			}
			
		} catch (Exception e) {
			expts.add(e);
			return false;
		}
	}
	
	
	public static boolean test_createClient(Client c, List<Exception> expts) {
		try {
			CallRawDB cr = new CallRawDB();
			cr.createClient(c);
			return true;
		} catch (Exception e) {
			expts.add(e);
			return false;
		}
	}
	
	
	public static boolean test_updateClient(Client c, List<Exception> expts) {
		c.setLogin("mouhaha");
		c.setEmail("monemail@haha.fr");
		try {
			CallRawDB cr = new CallRawDB();
			cr.updateClient(c);
			return true;
		} catch (Exception e) {
			expts.add(e);
			return false;
		}
	}
	
	public static boolean test_deleteClient(Client c, List<Exception> expts) {
		try {
			CallRawDB cr = new CallRawDB();
			cr.deleteClient(c);
			return true;
		} catch (Exception e) {
			expts.add(e);
			return false;
		}
	}
	
	public static boolean test_getAllClients(List<Exception> expts) {
		try {
			CallRawDB cr = new CallRawDB();
			List<Client> list = cr.getAllClients();
			if(list != null && list.size()!=0) {
				return true;
			} else {
				return false;
			}
			
		} catch (Exception e) {
			expts.add(e);
			return false;
		}
	}

	
	public static boolean test_createUserCharacter(UserCharacter uc, List<Exception> expts) {
		try {
			CallRawDB cr = new CallRawDB();
			cr.createUserCharacter(uc);
			return true;
		} catch (Exception e) {
			expts.add(e);
			return false;
		}
	}
	
	public static boolean test_updateUserCharacter(UserCharacter uc, List<Exception> expts) {
		uc.setCheveux(new Color(0,0,0));
		uc.setHabits(new Color(0,1,2));
		try {
			CallRawDB cr = new CallRawDB();
			cr.updateUserCharacter(uc);
			return true;
		} catch (Exception e) {
			expts.add(e);
			return false;
		}
	}
	
	public static boolean test_deleteUserCharacter(UserCharacter uc, List<Exception> expts) {
		try {
			CallRawDB cr = new CallRawDB();
			cr.deleteUserCharacter(uc);
			return true;
		} catch (Exception e) {
			expts.add(e);
			return false;
		}
	}
	
	public static boolean test_getAllUserCharacters(List<Exception> expts) {
		try {
			CallRawDB cr = new CallRawDB();
			List<UserCharacter> list = cr.getAllUserCharacters();
			if(list != null && list.size()!=0) {
				return true;
			} else {
				return false;
			}
			
		} catch (Exception e) {
			expts.add(e);
			return false;
		}
	}
	
	
}
